#ifndef __BLOCKING_RING_BUFFER_H__
#define __BLOCKING_RING_BUFFER_H__

#include "Semaphore.h"

typedef unsigned char BYTE;
enum RingBufferFetchResult { RBFR_OK = 0, RBFR_STOP = 1, RBFR_TOOLONG = 2 };
typedef enum RingBufferFetchResult En_RingBuffer_FetchResult;

class BlockingRingBuffer : private Semaphore
{
public:
	class Block
	{
	public:
		const static unsigned long BlockSize = 4 * 1024;
		Block *nextBlock = nullptr;
		BYTE *content = nullptr;
		Block();
		~Block();
	};
private:
	unsigned long ReadableRemain();
	unsigned long WritableRemain();
	bool Working = true;
	Block *pReadBlock, *pWriteBlock;
	BYTE *pReadPtr, *pWritePtr;
public:
	BlockingRingBuffer();
	~BlockingRingBuffer();
	void Cat(const BYTE *pData, unsigned long uLength);
	En_RingBuffer_FetchResult Fetch(BYTE *pData, unsigned long uLength);
	void Stop();
};

#endif  // __BLOCKING_RING_BUFFER_H__
